package view
{
	
	import assets.view.LoginViewSymbol;
	
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.text.TextFieldAutoSize;
	
	import model.vo.ViewVO;
	
	import view.components.GeneralButtonView;

	public class LoginView extends LoginViewSymbol
	{
		public static const NAME:String 				= "LoginView";
		
		public static const CLICKED:String 				= NAME + "Clicked";
		public static const SHOW:String					= NAME + "Show";
		public static const SUBMIT:String				= NAME + "Submit";
		
		public static const WRONG_LOGIN:String			= NAME + "WrongLogin";
		public static const SUCCESS_LOGIN:String		= NAME + "SuccessLogin";
		
		private var viewData:ViewVO;
		
		private var loginWidth:Number;
		private var loginHeight:Number;
		
		private var loginViewVecArt:Vector.<Vector3D>;
		
		private var submitBtn:GeneralButtonView;
		
		public function LoginView()
		{
		}
		
		public function init( viewVO:ViewVO ):void
		{
			viewData = viewVO;
			
			setLoginViewProperties();
			
			loginViewVecArt = new Vector.<Vector3D>();
			
			loginViewVecArt.push( new Vector3D( loginWidth, loginHeight ) );
			loginViewVecArt.push( new Vector3D( 0, loginHeight ) );
			loginViewVecArt.push( new Vector3D( 0, 0 ) );
			loginViewVecArt.push( new Vector3D( loginWidth, 0 ) );
			
			FX.spawnFrame( frameContainer, loginViewVecArt, loginSpawnCompleteHandler, viewData );
		}
		
		public function validateUser( u:Object ):void
		{
			welcomeLabel.autoSize = TextFieldAutoSize.LEFT;
			welcomeLabel.text = viewData.welcomeText;
			
			userWelcomeLabel.autoSize = TextFieldAutoSize.LEFT;
			userWelcomeLabel.text = u.name;
			
			submitBtn.die()
			
			welcomeLabel.x = Math.round( (this.width / 2) - ( welcomeLabel.width / 2) );
			userWelcomeLabel.x = Math.round( (this.width / 2) - ( userWelcomeLabel.width / 2) );
			
			FX.fadeIn( welcomeScreenContainer );
			FX.fadeOut( logDetailsContainer );
		}
		
		public function show():void
		{
			buildLoginView();
			
			userLabel.autoSize 		= TextFieldAutoSize.LEFT;
			userLabel.text 			= viewData.usernameText;
			
			passLabel.autoSize 		= TextFieldAutoSize.LEFT;
			passLabel.text 			= viewData.passwordText;
			
			submitBtn.x = Math.round( passInput.width  - submitBtn.width );
			
			FX.fadeIn( logDetailsContainer );
		}
		
		public function select():void
		{
			FX.fadeInOutTo( this, 0.2 );
		}
		
		public function unselect():void
		{
			userInput.text = "";
			passInput.text = "";
			
			FX.fadeInOutTo( this, 1 );
			submitBtn.reset();
		}
		
		private function setLoginViewProperties():void
		{
			loginWidth 		= viewData.LOGIN_WIDTH;
			loginHeight 	= viewData.LOGIN_HEIGHT;
			
			logDetailsContainer.alpha = 0;
			logDetailsContainer.addChild( userLabel );
			logDetailsContainer.addChild( userInput );
			logDetailsContainer.addChild( passLabel );
			logDetailsContainer.addChild( passInput );
			logDetailsContainer.addChild( submitBtnContainer );
			
			welcomeScreenContainer.alpha = 0;
			welcomeScreenContainer.addChild( welcomeLabel );
			welcomeScreenContainer.addChild( userWelcomeLabel );
			
			userInput.addEventListener( MouseEvent.MOUSE_DOWN, inputDownHandler);
			passInput.addEventListener( MouseEvent.MOUSE_DOWN, inputDownHandler);
		}
		
		private function buildLoginView():void
		{
			submitBtn = new GeneralButtonView();
			submitBtn.init( viewData, viewData.loginText );
			submitBtn.addEventListener( GeneralButtonView.CLICKED, submitBtnClickedHandler );
			submitBtnContainer.addChild( submitBtn );
			
		}
		
		private function submitBtnClickedHandler( e:Event ):void
		{
			if( userInput.text != "" && passInput.text != "" )
			{
				select();
				dispatchEvent( new Event( CLICKED ) );
				
				userInput.borderColor = 0x000000;
				passInput.borderColor = 0x000000;
				
				userInput.removeEventListener( MouseEvent.MOUSE_DOWN, inputDownHandler);
				passInput.removeEventListener( MouseEvent.MOUSE_DOWN, inputDownHandler);
			}
			else
			{
				submitBtn.reset();
				if(userInput.text == "") userInput.borderColor = 0xff0000;
				else userInput.borderColor = 0x000000;
				
				if(passInput.text == "") passInput.borderColor = 0xff0000;
				else passInput.borderColor = 0x000000;
			}
			
		}
		
		private function inputDownHandler( e:MouseEvent ):void
		{
			e.target.borderColor = 0x000000;
		}
		
		private function loginSpawnCompleteHandler():void
		{
			var g:Graphics = frameContainer.graphics;
			g.clear();
			g.lineStyle( viewData.LINE_WEIGHT, viewData.LINE_COLOR );
			g.drawRect( 0, 0, loginWidth, loginHeight );
		}
		
		public function get info():Object
		{
			return { username:userInput.text, password:passInput.text };
		}
	}
}